package com.platform.web.controller.module;

import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.core.text.Convert;
import com.platform.common.enums.BusinessType;
import com.platform.framework.util.ShiroUtils;
import com.platform.module.domain.BaseCalorimeterType;
import com.platform.module.domain.BaseFactory;
import com.platform.module.service.IBaseCalorimeterTypeService;
import com.platform.module.service.IBaseFactoryService;
import com.platform.system.service.ISysDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 热量型号Controller
 * 
 * @author xuwei
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/module/calorimeter")
public class BaseCalorimeterTypeController extends BaseController
{
    private String prefix = "module/calorimeter/type";

    @Autowired
    private IBaseCalorimeterTypeService baseCalorimeterTypeService;

    @Autowired
    private IBaseFactoryService baseFactoryService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @RequiresPermissions("module:calorimeter:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询热量型号数据列表
     */
    @RequiresPermissions("module:calorimeter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseCalorimeterType baseCalorimeterType)
    {
        startPage();
        List<BaseCalorimeterType> list = baseCalorimeterTypeService.selectBaseCalorimeterTypeList(baseCalorimeterType);
        return getDataTable(list);
    }

    /**
     * 新增热量型号数据
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        return prefix + "/add";
    }

    /**
     * 新增保存热量型号数据
     */
    @RequiresPermissions("module:calorimeter:add")
    @Log(title = "热量型号数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseCalorimeterType baseCalorimeterType)
    {
        baseCalorimeterType.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(baseCalorimeterTypeService.insertBaseCalorimeterType(baseCalorimeterType));
    }

    /**
     * 修改热量型号数据
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        BaseCalorimeterType baseCalorimeterType = baseCalorimeterTypeService.getById(typeId);
        mmap.put("baseCalorimeterType", baseCalorimeterType);
        return prefix + "/edit";
    }

    /**
     * 修改保存热量型号数据
     */
    @RequiresPermissions("module:calorimeter:edit")
    @Log(title = "热量型号数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseCalorimeterType baseCalorimeterType)
    {
        baseCalorimeterType.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(baseCalorimeterTypeService.updateBaseCalorimeterType(baseCalorimeterType));
    }

    /**
     * 删除热量型号数据
     */
    @RequiresPermissions("module:calorimeter:remove")
    @Log(title = "热量型号数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] idss = Convert.toStrArray(ids);
        for(String id : idss){
            if (baseCalorimeterTypeService.selectCountCalorimeterDataByTypeId(Long.parseLong(id)) > 0)
            {
                return AjaxResult.warn("存在子设备,不允许删除");
            }
        }
        return toAjax(baseCalorimeterTypeService.deleteBaseCalorimeterTypeByIds(ids));
    }

    /**
     * 校验热量表型号代码
     */
    @PostMapping("/checkCalorimeterTypeCodeUnique")
    @ResponseBody
    public String checkCalorimeterTypeCodeUnique(BaseCalorimeterType baseCalorimeterType)
    {
        return baseCalorimeterTypeService.checkCalorimeterTypeCodeUnique(baseCalorimeterType);
    }
}
